package arcane.object.screen;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import arcane.constant.Constants;
import arcane.network.ArcaneClient;
import arcane.network.RoomDetail;
import arcane.object.game.engine.ArcaneEngine;
import arcane.object.game.sprite.MenuSprite;
import arcane.object.gui.BevelBorder;
import arcane.object.gui.DefaultButton;
import arcane.object.gui.FrameWork;
import arcane.object.gui.GTGETextField;
import arcane.object.gui.scroll.ScrollPanel;
import arcane.object.gui.specific.ChatDisplayArea;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;

public class MultiplayerLoungeScreen extends GameObject implements
		ActionListener {
	/**
	 * Used in networking with host.
	 */
	protected ArcaneClient client;

	private GTGETextField txtChat;
	private FrameWork frame;

	private DefaultButton btnSend;
	private DefaultButton btnJoin;
	private DefaultButton btnCreate;
	private DefaultButton btnQuit;

	private ScrollPanel<String> chatArea;

	// GUI to display rooms client can join
	private ScrollPanel<RoomDetail> lstRooms;
	// GUI to display list of current clients
	private ScrollPanel<String> lstClients;

	public MultiplayerLoungeScreen(GameEngine engine) {
		super(engine);
	}

	public void setClient(ArcaneClient client) {
		this.client = client;
		new Thread(client).start();
		try {
			client.writeObject("NAME " + client.getDataID());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArcaneClient getClient() {
		return client;
	}

	public void initResources() {
		frame = new FrameWork(bsInput, 0, 435, 640, 45);
		txtChat = new GTGETextField(20, 440, 480, 30);
		txtChat.addActionListener(this);
		frame.add(txtChat);
		frame.setFocused(txtChat);

		chatArea = new ChatDisplayArea(5, 10, 390, 415);
		//for(int i = 0;i<100;i++)
			//chatArea.add("WILL: "+i);
		client.putObject(chatArea, Constants.MAIN_CHAT_REF);

		lstRooms = new ScrollPanel<RoomDetail>(420, 10,200,200);
		lstRooms.setData(client.getRoomNames());
		//lstRooms.setHeight(200);

		lstClients = new ScrollPanel<String>(420, 215,200,210);
		lstClients.setData(client.getPartyNames());
		//lstClients.setHeight(210);

		btnQuit = new DefaultButton("Quit");
		btnQuit.setLocation(575, 442);
		btnQuit.addActionListener(this);
		btnQuit.setBorder(new BevelBorder(BevelBorder.BEVEL_UP));
		btnQuit.setButtonBGColor(Color.BLUE);

		btnCreate = new DefaultButton("Create");
		btnCreate.addActionListener(this);

		btnJoin = new DefaultButton("Join");
		btnJoin.addActionListener(this);

		btnSend = new DefaultButton("Send");
		btnSend.setLocation(515, 442);
		btnSend.addActionListener(this);
		btnSend.setBorder(new BevelBorder(BevelBorder.BEVEL_UP));
		btnSend.setButtonBGColor(Color.BLUE);
	}

	public void render(Graphics2D g) {
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 640, 480);
		frame.render(g);
		btnSend.render(g);
		lstRooms.render(g);
		lstClients.render(g);
		btnQuit.render(g);
		chatArea.render(g);
	}

	public void update(long time) {
		this.chatArea.update(time);
		frame.update(time);
		btnSend.update(time);
		lstRooms.update(time);
		lstClients.update(time);
		btnQuit.update(time);
	}

	/*
	 * Sends command to server to add this data to the main display pane. This
	 * display pane is held in the script of all client's within the lounge
	 * screen. So, when the command is forwarded to only players within the
	 * lounge, it is valid within their script.
	 * 
	 * Text will only be forwarded to server if it contains chars other than
	 * just spaces.
	 */
	private void sendChatText() {
		String words = txtChat.getText();
		txtChat.setText("");
		if (words.length() > 0) {
			char[] ary = words.toCharArray();
			boolean flag = false;
			for (int i = 0; !flag && i < ary.length; i++)
				if (ary[i] != ' ')
					flag = true;
			if (flag)
				try {
					client
							.writeObject(Constants.MAIN_CHAT_REF + ".add(\""
									+ client.getDataID() + ": " + words.trim()
									+ "\");");
				} catch (IOException e2) {
					e2.printStackTrace();
				}
		}
	}

	public void actionPerformed(ActionEvent e) {
		String s = e.getActionCommand();
		if (e.getSource() == this.txtChat && e.getID()==MenuSprite.UNSPECIFIED) {
			sendChatText();
		}
		if (e.getID() == MenuSprite.MOUSE_DOWN) {
			if (s.equals("Send")) {
				sendChatText();
			} else if (s.equals("Join"))
				;
			else if (s.equals("Create"))
				;
			else if (s.equals("Quit")) {
				try {
					client.getSocket().close();
				} catch (IOException e2) {
					e2.printStackTrace();
				}
				this.parent.nextGameID = ArcaneEngine.MENU_SCREEN;
				finish();
			}

		}
	}
}